package com.itheima.test5;

import java.io.*;
import java.util.ArrayList;

/**
 * @Author:gy
 * @Date: 2025/08/31 16:27
 * @Description:
 */
public class Demo4 {
    public static void main(String[] args) throws Exception {
        // 姓名  年龄   学号   创建3个学生对象存到集合中  序列化到本地  最后在反序列化到控制台
        Student student1 = new Student("小帅",25,"heima001");
        Student student2 = new Student("小美",18,"heima002");
        Student student3 = new Student("小化",23,"heima003");
        Student student4 = new Student("小华",29,"heima004");
        //序列化  对象写到硬盘
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day11\\h.txt"));
        oos.writeObject(student1);
        oos.writeObject(student2);
        oos.writeObject(student3);
        oos.writeObject(student4);
        oos.close();
        //反序列化  硬盘上的字节码文件读取成对象
        //反序列化  硬盘上的字节码文件读取成对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Day11\\h.txt"));
        while (true) {
            //读不到就报错
            try {
                Student s1 = (Student) ois.readObject();
                System.out.println(s1);
            } catch (EOFException e) {
               break;
            }
        }

        ois.close();
    }
}
